// 观察者
class Watcher{
    constructor(vm,key,cb){
        // 把当前新生成的watcher添加到Dep类中
        Dep.target = this

        this.vm = vm
        this.key = key
        // 回调更新视图
        this.cb = cb
        this.oldVal = this.getObjData(this.vm.$data,this.key)

        // 触发vm.$data[key]的get方法后，清除Dep的watcher
        Dep.target = null
    }

    update(){
        let newVal = this.getObjData(this.vm.$data,this.key)
        if(newVal === this.oldVal) return 
        this.cb(newVal)
    }

    getObjData(data,key){
        if(/\./.test(key)){
            return eval('data.'+key)
        }else {
            return data[key]
        }
    }
}